﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoplanetaTaskManager.Models;
using AutoplanetaTaskManager.Utilities;
using AutoplanetaTaskManager.Views.Components;

namespace AutoplanetaTaskManager.Daos.EntityFramework
{
    public class TaskDao : ITaskDao
    {
        public List<Task> Load()
        {
            using (AtmDbEntities db = new AtmDbEntities())
                return db.Task.ToList();
        }

        public Task Get(int id)
        {
            using (AtmDbEntities db = new AtmDbEntities())
                return db.Task.FirstOrDefault(m => m.Id == id);
        }

        public Task Get(int postId, DateTime dateTime)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                return db.Task.FirstOrDefault(m => m.PostId == postId && m.TaskDateTime == dateTime);
            }
        }

        public void Add(Task task)
        {
            task.TaskDateTime = task.TaskDateTime;
            using (AtmDbEntities db = new AtmDbEntities())
            {
                db.AddToTask(task);
                db.SaveChanges();
            }
        }

        public void Update(Task task)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                Task updateTask = db.Task.FirstOrDefault(m => m.Id == task.Id);
                if (updateTask != null)
                {
                    updateTask.PostId = task.PostId;
                    updateTask.PublisherId = task.PublisherId;
                    updateTask.ClientName = task.ClientName;
                    updateTask.TaskDateTime = task.TaskDateTime;
                    updateTask.CountHours = task.CountHours;
                    updateTask.Description = task.Description;
                    updateTask.Finished = task.Finished;
                }

                db.SaveChanges();
            }
        }

        public void Remove(int id)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                Task task = db.Task.FirstOrDefault(m => m.Id == id);
                if (task != null) db.DeleteObject(task);
                db.SaveChanges();
            }
        }

        public bool CheckAbilityAdding(Task task)
        {
            for (
                DateTime dateTime = (DateTime)task.TaskDateTime;
                dateTime < (DateTime)task.TaskDateTime + TimeSpan.FromHours((double)task.CountHours);
                dateTime += TimeSpan.FromMinutes(30))
            {
                if (task.PostId != null && Get((int)task.PostId, dateTime) != null)
                    return false;
            }
            return true;
        }

        public bool CheckAbilityUpdating(Task task)
        {
            for (
                DateTime dateTime = (DateTime)task.TaskDateTime;
                dateTime < (DateTime)task.TaskDateTime + TimeSpan.FromHours((double)task.CountHours);
                dateTime += TimeSpan.FromMinutes(30))
            {
                Task taskFromDb = Get((int) task.PostId, dateTime);
                if (task.PostId != null && taskFromDb != null && task.Id != taskFromDb.Id)
                    return false;
            }
            return true;
        }
    }
}
